// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import uView from '@/uni_modules/uview-ui'
import {
	encrypt,
	decrypt
} from './utils/encryp.js';
import CryptoJS from "./utils/crypto-js-4.1.1/crypto-js.js";

Vue.use(uView)
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
export default app;
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
import messageVue from './pages/message/message.vue';
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif


/**
 * 加密和解密方法
 */
Vue.prototype.encrypt = encrypt;
Vue.prototype.decrypt = decrypt;

/**
 * 返回服务器 url
 */
Vue.prototype.getUrl = function() {
	// // #ifdef APP-PLUS
	// return "http://192.168.137.1:8088";
	// // #endif
	// // #ifdef H5
	// return "http://127.0.0.1:8088";
	// // #endif
	return "http://loac.cc:8088";
}

/**
 * 验证邮箱是否正确
 * @param {Object} email
 */
Vue.prototype.checkEmail = function(email) {
	let reEmail = /^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
	return reEmail.test(email);
}


/**
 * 保存数据前先进行加密
 * @param {Object} key
 * @param {Object} data
 */
Vue.prototype.setData = function(key, data) {
	uni.setStorageSync(key, this.encrypt(data));
}

/**
 * 获取数据前先解密
 * @param {Object} key
 */
Vue.prototype.getData = function(key) {
	return this.decrypt(uni.getStorageSync(key));
}

/**
 * 获取登录 Token
 */
Vue.prototype.getLoginToken = function() {
	return this.getData("login_token");
}

/**
 * 保存登录 Token
 */
Vue.prototype.setLoginToken = function(token) {
	return this.setData("login_token", token);
}


/**
 * toast 方法封装
 */
Vue.prototype.toast = function(mTitle, mIcon = "none", mPosition = "bottom") {
	uni.showToast({
		title: mTitle,
		icon: mIcon,
		position: mPosition
	});
}

Vue.prototype.md5 = function(str) {
	return CryptoJS.MD5(str).toString();
}

/**
 * 根据服务端返回信息判断 Token 是否过期
 * @param {Object} msg
 */
Vue.prototype.isExpired = function(msg) {
	if (msg == "Token不存在或已过期") {
		console.log("登录过期");
		this.setLoginToken("");
		this.toast("登录已过期");
		uni.redirectTo({
			url: "/pages/index/index"
		});
	}
}

function add0(m) {
	return m < 10 ? '0' + m : m
}

/**
 * 时间戳格式化
 * @param {Object} time
 */
Vue.prototype.format = function(time) {
	//time是整数，否则要parseInt转换
	// var time = new Date(time);
	// var current = new Date();
	// var y = time.getFullYear();
	// var currentY = current.getFullYear();
	// var m = time.getMonth() + 1;
	// var d = time.getDate();
	// var h = time.getHours();
	// var mm = time.getMinutes();
	// // var s = time.getSeconds();
	// if (currentY == y) {
	// 	return add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm);
	// } else {
	// 	return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm);
	// }
	var result;
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - time;
	if (diffValue < 0) {
		return;
	}
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	if (monthC >= 1) {
		result = "" + parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前";
	} else if (dayC >= 1) {
		result = "" + parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前";
	} else {
		result = "刚刚";
	}
	return result;
}
